package com.study.exchange.direct;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSON;
import com.study.constant.Constant;
import com.study.entity.Log;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.utils.SerializationUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SendMessageController {
    @Resource
    RabbitTemplate rabbitTemplate;

    /**
     * 发送Map消息,路由key：info
     * 测试地址：http://127.0.0.1:8887/study/sendInfoMessage?log=info消息的内容
     *
     * @param log
     * @return
     */
    @GetMapping("/sendInfoMessage")
    public String sendInfoMessage(@RequestParam(value = "log") String log) {
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, String> msgMap = new HashMap<>();
        msgMap.put("messageId", messageId);
        msgMap.put("messageData", log);
        msgMap.put("createTime", createTime);
        String routingKey = Constant.DirectConstant.logLevel.info.name();
        rabbitTemplate.convertAndSend(Constant.DirectConstant.LOG_DIRECT_EXCHANGE, routingKey, msgMap, new CorrelationData(messageId));

//        SerializationUtils.serialize()
//        SerializationUtils.deserialize()
        // rabbitTemplate.convertAndSend(RabbitConstant.QUEUE_ADD_USER, (Object) userStr,new CorrelationData(UUID.randomUUID().toString()));

        return createTime + "-已发送info消息：" + JSON.toJSON(msgMap);
    }

    /**
     * 发送User消息,路由key：error
     * 测试地址：http://127.0.0.1:8887/study/sendErrorMessage?log=日暮苍山远，天寒白屋贫
     *
     * @param content
     * @return
     */
    @GetMapping("/sendErrorMessage")
    public String sendErrorMessage(@RequestParam(value = "content") String content) {
        Constant.DirectConstant.logLevel logLevel = Constant.DirectConstant.logLevel.error;
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Log logEntity = new Log();
        logEntity.setLogLevel(logLevel).setContent(content);

        /**** 发送对象消息对象的其他三种方式 ****/
        //方式一：
        // rabbitTemplate.convertAndSend(Constant.DirectConstant.LOG_DIRECT_EXCHANGE, routingKey, user, new CorrelationData(messageId));

        //方式二：
        byte[] serializeUser = SerializationUtils.serialize(logEntity);
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT);
        messageProperties.setContentLength(serializeUser.length);
        Message message = new Message(serializeUser, messageProperties);

        Message message2 = MessageBuilder.withBody(serializeUser)
                .setContentType(MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT)
                .setContentLength(serializeUser.length).build();
        rabbitTemplate.send(Constant.DirectConstant.LOG_DIRECT_EXCHANGE, logLevel.name(), message, new CorrelationData(messageId));
        return createTime + "-已发送error消息：" + JSON.toJSON(logEntity);
    }
}
